package com.longrise.android.byjk.plugins.tabfirst;

import android.graphics.Paint;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.model.HomeMultipleItem;
import com.longrise.common.utils.AppUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by callxkj on 2017/2/23.
 * 首页主体的recycle的adapter
 */

public class HomeRcvAdapter extends BaseMultiItemQuickAdapter<HomeMultipleItem, BaseViewHolder> {


    private List<EntityBean> mListBeans = new ArrayList<>();
    private onItemTypeClickListener mTypeClickListener;


    public void setData(List<HomeMultipleItem> data, EntityBean[] beans) {
        mListBeans.clear();
        for (EntityBean bean : beans) {
            mListBeans.add(bean);
        }
        setNewData(data);
    }

    /**
     * 设置 新手专区 保险头条里面的image的点击事件
     */
    public void setOnItemTypeClickListener(onItemTypeClickListener listener) {
        this.mTypeClickListener = listener;
    }

    public HomeRcvAdapter(List<HomeMultipleItem> data) {
        super(data);
        addItemType(HomeMultipleItem.TYPE1, R.layout.homefragment_item1);//新手专区样式
        addItemType(HomeMultipleItem.TYPE2, R.layout.homefragment_item2);//保险头条样式
        addItemType(HomeMultipleItem.TYPE3, R.layout.homefragment_item3);//横排有一个图片的样式
        addItemType(HomeMultipleItem.TYPE4, R.layout.homefragment_item4);//横排两个下面一个一共三个图片的样式
        addItemType(HomeMultipleItem.TYPE_DEFAULT, R.layout.homefragment_item_default);// 默认的空样式 用于服务端传了不明的类型
        addItemType(HomeMultipleItem.TYPE7, R.layout.homefragment_item7); //免费试看
        addItemType(HomeMultipleItem.TYPE8, R.layout.homefragment_item8); //单张图片的样式 演示视频
        addItemType(HomeMultipleItem.TYPE9, R.layout.homefragment_item9); //批量购买
        addItemType(HomeMultipleItem.TYPE10, R.layout.homefragment_item10); //首页hra检测名额

    }

    @Override
    protected void convert(BaseViewHolder helper, HomeMultipleItem item) {
        int adapterPosition = helper.getAdapterPosition();
        EntityBean entityBean = mListBeans.get(adapterPosition - 1);
        View convertView = helper.getConvertView();
        String areaname = entityBean.getString("areaname");
        EntityBean[] childmenus = entityBean.getBeans("childmenus");
        String loadmoretype = entityBean.getString("isshowmore");
        String style = entityBean.getString("style");
        switch (helper.getItemViewType()) {
            case HomeMultipleItem.TYPE1: //新手专区样式的item
                parseType1(childmenus, convertView, areaname);
                break;
            case HomeMultipleItem.TYPE2: //保险头条样式的item 带有字体
                parseType2(childmenus, convertView, areaname, loadmoretype);
                break;
            case HomeMultipleItem.TYPE3: //横排有两个图片的样式 宝易工具(一个区域)
            case HomeMultipleItem.TYPE4: //横排两个下面一个一共三个图片的样式 宝易工具(三个区域)
            case HomeMultipleItem.TYPE5: // 宝易工具(五个区域)
                parseType345(childmenus, convertView, style, item);
                break;
            case HomeMultipleItem.TYPE7: //免费试看的样式
                parseType7(helper, areaname, loadmoretype);
                break;
            case HomeMultipleItem.TYPE8: //单张图片样式
                parseType8(helper, entityBean);
                break;
            case HomeMultipleItem.TYPE9: //批量购买
                parseType9(helper, areaname, loadmoretype);
                break;
            case HomeMultipleItem.TYPE10: //首页hra检测名额
                parseType10(helper);
                break;
        }
    }

    /**
     * 处理type1(新手专区) 数据
     */
    private void parseType1(EntityBean[] childmenuses, View convertView, String areaname) {

        RecyclerView recyclerView = (RecyclerView) convertView.findViewById(R.id.homefragment_item1_rcv);
        TextView mTvTitle = (TextView) convertView.findViewById(R.id.homefragment_item1_title);
        mTvTitle.setText(areaname);
        HomeRcvItem1Adapter adapter = new HomeRcvItem1Adapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter);
        adapter.setOnItem1ClickListener(new HomeRcvItem1Adapter.OnItem1ClickListener() {
            @Override
            public void onClick(String url, String title, String linkedtype) {
                if (mTypeClickListener != null) {
                    mTypeClickListener.onClick(HomeMultipleItem.TYPE1, url, title, linkedtype, null);
                }
            }
        });

        View view = View.inflate(AppUtil.getContext(), R.layout.item_homercvitem_footview, null);
        adapter.addFooterView(view, 0, LinearLayout.HORIZONTAL);
        adapter.refreshData(childmenuses);
    }

    /**
     * 处理type2(保宝头条) 数据
     */
    private void parseType2(EntityBean[] childmenuses, View convertView, String areaname, String loadmoretype) {

        RecyclerView recyclerView2 = (RecyclerView) convertView.findViewById(R.id.homefragment_item2_rcv);
        TextView mTvTitle = (TextView) convertView.findViewById(R.id.homefragment_item2_title);
        RelativeLayout mRlrightTitle = (RelativeLayout) convertView.findViewById(R.id.homefragment_item2_more_rl);
        mTvTitle.setText(areaname);
        //1 表示显示查看全部按钮,即显示查看全部
        if ("1".equals(loadmoretype)) {
            mRlrightTitle.setVisibility(View.VISIBLE);
            mRlrightTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mTypeClickListener != null) {
                        mTypeClickListener.onClick(HomeMultipleItem.TYPE2_1, null, null, null, null);
                    }
                }
            });
        } else {
            mRlrightTitle.setVisibility(View.GONE);
            mRlrightTitle.setOnClickListener(null);
        }

        HomeRcvItem2Adapter adapter2 = new HomeRcvItem2Adapter();
        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(mContext);
        linearLayoutManager2.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView2.setLayoutManager(linearLayoutManager2);
        recyclerView2.setAdapter(adapter2);
        adapter2.setOnItem2ClickListener(new HomeRcvItem2Adapter.OnItem2ClickListener() {
            @Override
            public void onClick(String url, String title, String linkedtype) {
                if (mTypeClickListener != null) {
                    mTypeClickListener.onClick(HomeMultipleItem.TYPE2, url, title, linkedtype, null);
                }
            }
        });

        View view = View.inflate(AppUtil.getContext(), R.layout.item_homercvitem_footview, null);
        adapter2.addFooterView(view, 0, LinearLayout.HORIZONTAL);
        adapter2.refreshData(childmenuses);
    }

    /**
     * 宝易3种 集中处理
     */
    private void parseType345(EntityBean[] childmenus, View convertView, final String style,
                              HomeMultipleItem item) {
        //动态设置区域高度
        int screenwidth = AppUtil.getScreenWidth();
        int itemType = item.getItemType();
        switch (itemType) {
            case HomeMultipleItem.TYPE4:
                LinearLayout ll = (LinearLayout) convertView.findViewById(R.id.home_fragment_item4_ll1);
                ImageView iv = (ImageView) convertView.findViewById(R.id.home_fragment_item4_iv3);
                int llHeight = (int) (((float) 120 / 375) * screenwidth);
                ViewGroup.LayoutParams layoutParamsll = ll.getLayoutParams();
                layoutParamsll.height = llHeight;
                ll.setLayoutParams(layoutParamsll);
                int ivHeight = (int) (((float) 150 / 375) * screenwidth);
                ViewGroup.LayoutParams layoutParamsiv = iv.getLayoutParams();
                layoutParamsiv.height = ivHeight;
                iv.setLayoutParams(layoutParamsiv);
                break;
        }

        for (int x = 0; x < childmenus.length; x++) {
            final String uniqueid = childmenus[x].getString("uniqueid");
            String picpath = childmenus[x].getString("picpath");
            final String linkedtype = childmenus[x].getString("linkedtype");
            String linkedurl = childmenus[x].getString("linkedurl");
            final String text = childmenus[x].getString("text");
            switch (x) {
                case 0:
                    ImageView iv1 = (ImageView) convertView.findViewById(R.id.home_fragment_item4_iv1);
                    Glide.with(AppUtil.getContext()).load(picpath).into(iv1);
                    iv1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if ("1005".equals(style) && mTypeClickListener != null) {
                                mTypeClickListener.onClick(HomeMultipleItem.TYPE4, null, text,
                                        linkedtype, uniqueid);
                            }
                        }
                    });
                    break;
                case 1:
                    ImageView iv2 = (ImageView) convertView.findViewById(R.id.home_fragment_item4_iv2);
                    Glide.with(AppUtil.getContext()).load(picpath).into(iv2);
                    iv2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if ("1005".equals(style) && mTypeClickListener != null) {
                                mTypeClickListener.onClick(HomeMultipleItem.TYPE4, null, text,
                                        linkedtype, uniqueid);
                            }
                        }
                    });
                    break;
                case 2:
                    ImageView iv3 = (ImageView) convertView.findViewById(R.id.home_fragment_item4_iv3);
                    Glide.with(AppUtil.getContext()).load(picpath).into(iv3);
                    iv3.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if ("1005".equals(style) && mTypeClickListener != null) {
                                mTypeClickListener.onClick(HomeMultipleItem.TYPE4, null, text,
                                        linkedtype, uniqueid);
                            }
                        }
                    });
                    break;
            }
        }
    }

    /**
     * 免费试看的样式
     */
    private void parseType7(final BaseViewHolder helper, String areaname, String loadmoretype) {
        helper.setVisible(R.id.homefragment_item7_root, true);
        //标题
        TextView mTvtitle = helper.getView(R.id.homefragment_item7_title);
        mTvtitle.setText(areaname);

        //1 表示显示查看全部按钮,即显示查看更多
        RelativeLayout mRlcheckmore = helper.getView(R.id.homefragment_item7_more_rl);
        if ("1".equals(loadmoretype)) {
            mRlcheckmore.setVisibility(View.VISIBLE);
            mRlcheckmore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mTypeClickListener != null) {
                        mTypeClickListener.onClick(HomeMultipleItem.TYPE7_1, null, null,
                                null, null);
                    }
                }
            });
        } else {
            mRlcheckmore.setVisibility(View.GONE);
            mRlcheckmore.setOnClickListener(null);
        }

        final RecyclerView rcv = helper.getView(R.id.homefragment_item7_rcv);
        HomeGetDataHelper.getFreeCourse(new HomeGetDataHelper.BatchbuyResult() {
            @Override
            public void onSuccess(EntityBean[] beans) {
                HomeRcvItem7Adapter adapter7 = new HomeRcvItem7Adapter();
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
                linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                rcv.setLayoutManager(linearLayoutManager);
                rcv.setAdapter(adapter7);
                adapter7.refreshData(beans);
                adapter7.setOnItem7ClickListener(new HomeRcvItem7Adapter.OnItem7ClickListener() {
                    @Override
                    public void onClick(String courseid, String realprice) {
                        if (mTypeClickListener != null) {
                            mTypeClickListener.onClick(HomeMultipleItem.TYPE7_2, courseid, realprice,
                                    null, null);
                        }
                    }
                });
            }

            @Override
            public void onError() {
                helper.setVisible(R.id.homefragment_item7_root, false);
            }
        });

    }

    /**
     * 单张图片跳转到公司的视频介绍页面
     */
    private void parseType8(BaseViewHolder helper, EntityBean bean) {
        String imgurl = bean.getString("imgurl");
        final String videourl = bean.getString("videourl");
        final String areaname = bean.getString("areaname");
        ImageView iv = helper.getView(R.id.homefragment_item8_iv);
        int width = AppUtil.getScreenWidth() - AppUtil.dip2px(32);
        int height = (int) (width * (float) 11 / 34);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) iv.getLayoutParams();
        layoutParams.height = height;
        iv.setLayoutParams(layoutParams);

        Glide.with(AppUtil.getContext()).load(imgurl).into(iv);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mTypeClickListener != null) {
                    mTypeClickListener.onClick(HomeMultipleItem.TYPE8_1, videourl, areaname,
                            null, null);
                }
            }
        });
    }

    /**
     * 批量购买
     */
    private void parseType9(final BaseViewHolder helper, String areaname, String loadmoretype) {
        //标题
        TextView mTvtitle = helper.getView(R.id.homefragment_item9_title);
        mTvtitle.setText(areaname);

        //1 表示显示查看全部按钮,即显示查看更多
        RelativeLayout mRlcheckmore = helper.getView(R.id.homefragment_item9_more_rl);
        if ("1".equals(loadmoretype)) {
            mRlcheckmore.setVisibility(View.VISIBLE);
            mRlcheckmore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mTypeClickListener != null) {
                        mTypeClickListener.onClick(HomeMultipleItem.TYPE9_1, null, null,
                                null, null);
                    }
                }
            });
        } else {
            mRlcheckmore.setVisibility(View.GONE);
            mRlcheckmore.setOnClickListener(null);
        }

        //立即购买
        RelativeLayout mRlbuy = helper.getView(R.id.homefragment_item9_buy);
        mRlbuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mTypeClickListener != null) {
                    mTypeClickListener.onClick(HomeMultipleItem.TYPE9_2, null, null,
                            null, null);
                }
            }
        });

        final LinearLayout ll = helper.getView(R.id.homefragment_item9_ll);
        final View rootView = helper.getView(R.id.homefragment_item9_ll_root);
        final ViewGroup.LayoutParams layoutParams = rootView.getLayoutParams();
        HomeGetDataHelper.getBatchbuydata(new HomeGetDataHelper.BatchbuyResult() {
            @Override
            public void onSuccess(EntityBean[] beans) {
                ll.removeAllViews(); //先将子view 全部移除掉
                if (beans == null || beans.length == 0) {
                    rootView.setVisibility(View.GONE);
                    layoutParams.height = 0;  //没有数据的时候隐藏这个item. 高度改为0 便于rcv计算高度
                    rootView.setLayoutParams(layoutParams);
                } else {
                    rootView.setVisibility(View.VISIBLE);
                    layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                    rootView.setLayoutParams(layoutParams);
                    for (int x = 0; x < beans.length; x++) {
                        EntityBean bean = beans[x];
                        String traintypename = bean.getString("grouptypename");
                        String name = bean.getString("groupname");
                        String originprice = bean.getString("originprice");
                        String realprice = bean.getString("realprice");
                        String infosummary = bean.getString("infosummary");
                        String maxprice = bean.getString("maxprice");
                        String minprice = bean.getString("minprice");

                        View itemview = View.inflate(mContext, R.layout.homefragment_item9_item, null);
                        TextView tvxt = (TextView) itemview.findViewById(R.id.homefragment_item9_xitong);
                        TextView tvcoursename = (TextView) itemview.findViewById(R.id.homefragment_item9_coursename);
                        TextView tvdesc = (TextView) itemview.findViewById(R.id.homefragment_item9_desc);
                        TextView tvrealprice = (TextView) itemview.findViewById(R.id.homefragment_item9_realprice);
                        TextView tvoriginprice = (TextView) itemview.findViewById(R.id.homefragment_item9_originprice);
                        tvoriginprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

                        tvxt.setText(traintypename);
                        tvcoursename.setText(name);
                        tvrealprice.setText("¥" + minprice + "~" + "¥" + maxprice);
                        tvoriginprice.setText("¥" + originprice);
                        tvdesc.setText(infosummary);
                        ll.addView(itemview);

                        //每个item的点击 都跳转到 购买全部的订单确认界面  与立即购买一样
                        itemview.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (mTypeClickListener != null) {
                                    mTypeClickListener.onClick(HomeMultipleItem.TYPE9_1, null, null,
                                            null, null);
                                }
                            }
                        });
                    }
                }

            }

            @Override
            public void onError() {
                rootView.setVisibility(View.GONE);
                layoutParams.height = 0;
                rootView.setLayoutParams(layoutParams);
            }
        });
    }

    /**
     * hra预约名额
     */
    private void parseType10(final BaseViewHolder helper) {
        final View rootView = helper.getView(R.id.homefragment_item10_rl_root);
        final ViewGroup.LayoutParams layoutParams = rootView.getLayoutParams();
        HomeGetDataHelper.getHraData(new HomeGetDataHelper.HraResult() {
            @Override
            public void onSuccess(boolean isShow) {
                if (isShow) {
                    rootView.setVisibility(View.VISIBLE);
                    layoutParams.height = AppUtil.dip2px(85);
                } else {
                    rootView.setVisibility(View.GONE);
                    layoutParams.height = 0;
                }
                rootView.setLayoutParams(layoutParams);
            }

            @Override
            public void onError() {
                rootView.setVisibility(View.GONE);
                layoutParams.height = 0;
                rootView.setLayoutParams(layoutParams);
            }
        });

        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mTypeClickListener != null) {
                    mTypeClickListener.onClick(HomeMultipleItem.TYPE10_1, null, null,
                            null, null);
                }
            }
        });
    }


    interface onItemTypeClickListener {
        void onClick(int type, String url, String text, String linkedtype, String uniqueid);
    }
}
